package com.ljc.service;

import com.ljc.dao.DevicePurchaseDemandDao;
import com.ljc.dao.ProjectPlanningDao;
import com.ljc.dao.PurchaseApplyDao;
import com.ljc.dao.PurchaseApplyReviewDao;
import com.ljc.entity.DevicePurchaseDemand;
import com.ljc.entity.ProjectPlanning;
import com.ljc.entity.PurchaseApply;
import com.ljc.entity.PurchaseApplyReview;
import com.ljc.vo.PurchaseApplySearByUser;
import com.ljc.vo.PurchaseApplySearchByAdmit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.PipedReader;
import java.util.Date;
import java.util.List;
@Service
public class PurchaseApplyServiceImpl implements PurchaseApplyService{
    @Autowired
    private PurchaseApplyDao purchaseApplyDao;

    @Autowired
    private DevicePurchaseDemandDao devicePurchaseDemandDao;

    @Autowired
    private ProjectPlanningDao projectPlanningDao;

    @Autowired
    private PurchaseApplyReviewDao purchaseApplyReviewDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insert(PurchaseApply purchaseApply) {
        purchaseApply.setStatus(1);
        purchaseApply.setCreatedDate(new Date());
        int id = purchaseApplyDao.insert(purchaseApply);
        List<DevicePurchaseDemand> devicePurchaseDemands = purchaseApply.getDevicePurchaseDemands();
        for (DevicePurchaseDemand devicePurchaseDemand : devicePurchaseDemands) {
            devicePurchaseDemand.setPurchaseApplyId(purchaseApply.getId());
            devicePurchaseDemandDao.insert(devicePurchaseDemand);
        }
        projectPlanningDao.setStatus(purchaseApply.getProjectPlanningId(),13);
        return id;
    }

    @Override
    public List<PurchaseApply> searchByUser(PurchaseApplySearByUser search) {
        return purchaseApplyDao.searchByUser(search);
    }

    @Override
    public PurchaseApply findById(int id) {
        PurchaseApply purchaseApply = purchaseApplyDao.findById(id);
        List<DevicePurchaseDemand> byPurchaseApply = devicePurchaseDemandDao.findByPurchaseApply(id);
        purchaseApply.setDevicePurchaseDemands(byPurchaseApply);
        return purchaseApply;
    }

    @Override
    public List<PurchaseApply> findByAdmit(PurchaseApplySearchByAdmit search) {
        return purchaseApplyDao.searchByAdmit(search);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void review(PurchaseApplyReview review) {
        review.setDate(new Date());
        purchaseApplyReviewDao.insert(review);
        if(review.getResult()==1){
            purchaseApplyDao.setStatus(review.getPurchaseApplyId(),3);
        }if (review.getResult()==2){
            purchaseApplyDao.setStatus(review.getPurchaseApplyId(),2);
        }
    }

    @Override
    public PurchaseApplyReview getReview(int id) {
        return purchaseApplyReviewDao.searchByPurchaseId(id);
    }

    @Override
    public int countByUserId(int userId) {
        return purchaseApplyDao.countByUserID(userId);
    }
}
